﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushBox.Core
{
    /// <summary>
    /// 玩家小人
    /// </summary>
    public class Player : Position
    {
        public void Move(MoveType moveType)
        {
            switch (moveType)
            {
                case MoveType.Up:
                    Y--;
                    break;
                case MoveType.Down:
                    Y++;
                    break;
                case MoveType.Left:
                    X--;
                    break;
                case MoveType.Right:
                    X++;
                    break;
            }
        }

        /// <summary>
        /// 能否移动
        /// </summary>
        /// <param name="moveType"></param>
        public bool TryMove(MoveType moveType)
        {
            Position p = null;
            switch (moveType)
            {
                case MoveType.Up:
                    p = this.Map.GetObject(this.X, this.Y - 1);
                    break;
                case MoveType.Down:
                    p = this.Map.GetObject(this.X, this.Y + 1);
                    break;
                case MoveType.Left:
                    p = this.Map.GetObject(this.X - 1, this.Y);
                    break;
                case MoveType.Right:
                    p = this.Map.GetObject(this.X + 1, this.Y);
                    break;
            }
            if (p is Wall) return false;
            if (p is Box)
            {
                Box b = (Box)p;
                //long -> int 强制类型转换
                //父类 -> 子类 强制类型转换
                bool boxCanMove = b.TryMove(moveType);
                if (!boxCanMove) return false;
            }
            this.Move(moveType);
            // 重新在屏幕上画地图
            this.Map.DoShowMap(this.Map);
            this.Map.CheckPass();
            return true;
        }
    }
}
